<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\ModuleService;
use  App\V3\V3_1\Models\ModuleModel;
use Illuminate\Http\Request;
use Validator;

class ModuleController extends BaseController
{
    /**
     * @权限列表
     * created 在 2021/11/13 16:49 创建
     * @author SunWu
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request)
    {
        if($this->getUserId() != 1) {
            return $this->fail('', '权限不足');
        }

        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        return $this->success(ModuleService::moduleList($data));
    }

    /**
     * @权限信息列表
     * created 在 2021/11/20 14:37 创建
     * @author SunWu
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function select()
    {
        if($this->getUserId() != 1) {
            return $this->fail('', '权限不足');
        }

        $data = ModuleModel::select('id', 'module_name')
                            ->where([
                                'delete_flag' => 0,
                                'upper_module_id' => 0
                            ])
                            ->orderBy('sort', 'asc')
                            ->orderBy('id', 'desc')
                            ->get();

        return $this->success($data);
    }

    /**
     * @增加权限模块
     * created 在 2021/11/20 10:34 创建
     * @author SunWu
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request)
    {
        if($this->getUserId() != 1) {
            return $this->fail('', '权限不足');
        }

        //参数验证
        $validator = Validator::make($data = $request->all(), [
                'module_name' =>'required',//权限模块名
                'sort' =>'required|integer|min:0',//排序
                'upper_module_id' =>'integer|min:0',//类型
            ],
            [
                'module_name.required' => '参数`权限模块名`缺失',
                'sort.integer' => '参数`排序`只能为整型',
            ]
        );

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $this->log('新建权限模块', $data);

        return response()->json(ModuleService::add($data));
    }

    /**
     * @删除权限模块
     * created 在 2021/11/20 10:56 创建
     * @author SunWu
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function del(Request $request)
    {
        if($this->getUserId() != 1) {
           return $this->fail('', '权限不足');
        }

        //参数验证
        $validator = Validator::make($data = $request->all(), [
                'id' =>'required|integer',//权限模块名
            ],
            [
                'id.required' => '参数`权限模块id`缺失',
            ]
        );

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $checkInfo = ModuleModel::where('id', $data['id'])->first();

        if($checkInfo['upper_module_id'] == 0) {
            if(ModuleModel::where(['delete_flag' => 0, 'upper_module_id' => $data['id']])->first()) {
                return response()->json([
                    'code' => 1,
                    'msg' => '存在未删除的二级权限模块，无法删除',
                    'data' => '',
                ]);
            }
        }

        $this->log('删除权限模块', $data);

        ModuleModel::where('id', $data['id'])->update(['delete_flag' => 1]);

        return $this->success();
    }

    /**
     * @修改权限模块
     * created 在 2021/11/20 11:05 创建
     * @author SunWu
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request)
    {
        if($this->getUserId() != 1) {
            return $this->fail('', '权限不足');
        }

        //参数验证
        $validator = Validator::make($data = $request->all(), [
                'id' =>'required|integer',//权限模块名
                'module_name' =>'required',//权限模块名
                'sort' =>'integer|min:0',//排序
                'upper_module_id' =>'integer|min:0',//类型
            ],
            [
                'id.required' => '参数`权限模块id`缺失',
                'module_name.required' => '参数`权限模块名`缺失',
                'sort.integer' => '参数`排序`只能为整型',
                'upper_module_id.integer' => '参数`上级模块权限id`只能为整型',
            ]
        );

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $this->log('修改权限模块', $data);

        return response()->json(ModuleService::edit($data));
    }
}
